package com.zmn.oms.dubbo.impl.normal.captcha;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.common.dio.normal.captcha.GetVerificationCodeDIO;
import com.zmn.oms.common.dio.normal.captcha.ValidateVerificationCodeDIO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.interfaces.normal.captcha.CaptchaRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.zmn.business.interfaces.captcha.OrderWorkCaptchaBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 类描述：验证码服务
 *
 * @author liuyufan
 * @since 2021/12/01
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class CaptchaRemoteServiceImpl implements CaptchaRemoteService {

    @Autowired
    private OrderWorkCaptchaBService orderWorkCaptchaBService;

    /**
     * 发送验证码给用户
     *
     * @param getVerificationCodeDIO
     * @return
     */
    @Override
    public ResponseDTO sendVerificationCodeToUser(GetVerificationCodeDIO getVerificationCodeDIO) {
        log.info("发送短信验证码入参：[{}]", JSON.toJSONString(getVerificationCodeDIO));
        try {
            orderWorkCaptchaBService.sendVerificationCodeToUser(getVerificationCodeDIO);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送短信验证码失败：{}", e.getMessage());
            return ResponseDTO.fail();
        }
        return ResponseDTO.success();
    }

    /**
     * 验证订单验证码
     *
     * @param validateVerificationCodeDIO
     * @return
     */
    @Override
    public ResponseDTO validateVerificationCode(ValidateVerificationCodeDIO validateVerificationCodeDIO) {
        log.info("校验短信验证码入参：[{}]", JSON.toJSONString(validateVerificationCodeDIO));
        try {
            orderWorkCaptchaBService.validateVerificationCode(validateVerificationCodeDIO);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("校验短信验证码失败：{}", e.getMessage());
            return ResponseDTO.fail();
        }
        return ResponseDTO.success();
    }
}
